package com.yulian.util;

import java.util.regex.Pattern;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.codec.binary.Base64;

public class MyDesUtil {
	
	private final static String SECRET_KEY = "yulian@!"; 
	//pg password：
    private static String content = "123456";
    
    public static void main(String[] args) { 
        System.out.println("=======验证字符串是否为数值=====");
        String str1 = "234";
        String str2 = "234.2222";
        String str3 = "2e34.2222";
        boolean result1 = isNumericA(str1);
        boolean result2 = isNumericA(str2);
        boolean result3 = isNumericA(str3);
        System.out.println("=======result1=====: "+result1+ " result2:"+result2 + " result3:"+result3);
//        System.out.println("=======before encrypt=====");
//        System.out.println(content);
//        String encryptStr = encrypt(content, SECRET_KEY);
//        System.out.println("======afterencrypt ====");
//        System.out.println(encryptStr);
//        String decryptStr = decrypt(encryptStr, SECRET_KEY);
//        System.out.println("======after decrypt=====");
//        System.out.println(decryptStr);  
    } 
    /**
	 * 用ASCII判断字符串是否为整数或者小数，效率最高
	 *
	 * @param str
	 * @return
	 */
	public static boolean isNumericA(String str) {  
		StringBuilder stringBuilder = deleteChar(str);
		for (int i = stringBuilder.length(); --i >= 0; ) {
			int chr = stringBuilder.charAt(i);
			if (chr < 48 || chr > 57)
				return false;
		}
		return true;
	}

	public static StringBuilder deleteChar(String str) {
		int flag;
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(str);
//		if (stringBuilder.charAt(0) == '+' || stringBuilder.charAt(0) == '-') {
//			stringBuilder.deleteCharAt(0);
//		}
		if ((flag = stringBuilder.indexOf(".")) != -1) {
			stringBuilder.deleteCharAt(flag);
		}
		return stringBuilder;
	} 
  
    public static  String encrypt(String content,String secretKey) {
    	try {
            
            byte[] contentByte = content.getBytes();
            byte[] secretKeyByte = secretKey.getBytes();           
            DESKeySpec keySpec=new DESKeySpec(secretKeyByte);
            SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");            
            SecretKey key=keyFactory.generateSecret(keySpec);               
            Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(keySpec.getKey()));            
            byte[] result=cipher.doFinal(contentByte);
           
            String resultStr = new Base64().encodeToString(result);
            return resultStr;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("encrypt exception:"+e.toString());
        }
        return null; 
    }
         
    public static String decrypt(String content,String secretKey) {
        try {
            new Base64();
			byte [] contentByte =Base64.decodeBase64(content);
            byte [] secretKeyByte = secretKey.getBytes();
            DESKeySpec keySpec=new DESKeySpec(secretKeyByte);
            SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
            SecretKey key=keyFactory.generateSecret(keySpec);
            Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(secretKeyByte));
            byte[] result=cipher.doFinal(contentByte);
            String resultStr = new String (result);
            return resultStr;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("decrypt exception:"+e.toString());
        }
        return null;
    }  
}
